(() => {
    'use strict';

    const list = [
        "diy_146_162",
        "union_report_combinationdesc_c",
        "union_report_combinationname_c",
        "union_report_groupname_c",
        "union_report_homedesc_c",
        "union_report_mapdesc_c",
        "union_report_mapname_c",
        "union_report_name_c",
        "union_report_roomname_c",
        "union_report_talk_c",
        "上架",
        "高热",
        "举报",
        "封面"
    ]
    const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
    const main = async () => {
        console.log("start")
        const iframe = document.getElementById("__ace_app_container");
        if (!iframe) {
            await sleep(500)
            console.log("error2")
            await main();
            return;
        }
        const options = [];
        const iframeDocument = iframe.contentWindow.document
        const querysElem = Array.from(iframeDocument.getElementsByClassName("query-item")).slice(0, 4);
        for (const queryElem of querysElem) {
            queryElem.getElementsByTagName("input")[0].click();
        }
        await sleep(2000)
        const ul = Array.from(iframeDocument.querySelectorAll('.el-select-dropdown.el-popper'))
        const uls = ul.filter(e => e.style.display != 'none');

        if (uls.length !== 4) {
            console.log("error1")
            await main();
            return;
        }
        for (let i = 0; i < 4; i++) {
            const obj = {
                children: Array.from(uls[i].getElementsByTagName("li"))
            }
            const queryElem = querysElem[i];
            obj.type = queryElem.getElementsByClassName("search-label")[0].innerText;
            options.push(obj)
        }
        await sleep(500)
        for (const queryElem of querysElem) {
            queryElem.getElementsByTagName("input")[0].click();
        }

        const cardBtn = document.createElement("button");
        cardBtn.innerText = "点击打开面板 或右键启动"
        cardBtn.style.position = "fixed";
        cardBtn.style.right = "200px";
        cardBtn.style.bottom = "200px";
        cardBtn.className = "el-button"
        const container = document.createElement("div")
        container.innerHTML = `      
        
        
        <dialog id="locnelorDialog"
        style="width: 700px;height: 400px;position: fixed;left: calc(50%-350px);right: calc(50%-200px);background: #fff;border-radius: 10px;padding: 10px;">
        <div style="height: 350px;">
            <div>
                <textarea id="locnelorTextarea" name="" id="" cols="30" rows="10"></textarea>
            </div>
            <div>
                <select id="locnelorSelect"></select>
                <button id="locnelorAdd" class="el-button">添加</button>
                <label>
                    刷新时间（毫秒）
                    <input id="locnelorMs" type="text" value="3000">
                </label>
            </div>
            <div></div>
        </div>
        <div style="display: flex;justify-content: end;gap: 2;">
            <button id="locnelorBeginBtn" class="el-button">开始</button>
            <button id="locnelorCloseBtn" class="el-button">关闭</button>
        </div>
    </dialog>
    
    
    `
        const types = options.find((e) => e.type === "数据渠道").children;
        document.body.appendChild(container)
        document.body.appendChild(cardBtn)

        const dialog = document.getElementById("locnelorDialog")

        const select = document.getElementById("locnelorSelect");
        const textarea = document.getElementById("locnelorTextarea")
        const optionSet = new Set()
        const liOption = {}
        for (const li of types) {
            const option = document.createElement("option")
            option.value = li.innerText;
            option.innerText = li.innerText;
            liOption[li.innerText] = li;
            optionSet.add(li.innerText);
            select.appendChild(option);
        }

        let time;
        let time2;
        let running = false;
        const regex = /\d+/g;
        cardBtn.onclick = () => {
            const checks = textarea.value.split("\n").filter(e => !!e).filter(e => {
                return optionSet.has(e);
            });
            textarea.value = checks.join("\n");
            dialog.showModal()
        };
        textarea.value = list.join("\n");
        const begin = () => {
            if (running) return;
            running = true;
            const checks = textarea.value.split("\n").filter(e => !!e).filter(e => {
                return optionSet.has(e);
            });
            textarea.value = checks.join("\n");
            const lis = checks.map(e => liOption[e]);
            let i = 0;
            const tmp = parseInt(document.getElementById("locnelorMs")?.value);
            const ms = isNaN(tmp) ? 3000 : tmp;
            time2 = setInterval(async () => {
                const getButton = iframeDocument.getElementsByClassName("el-button el-button--default el-button--small")[0];
                if (getButton.disabled) return;
                lis[i].click();
                await sleep(200);
                const num = iframeDocument.getElementsByClassName("num")[0];
                const n = parseInt(num.innerText.match(regex)[0]);
                // if (n != 0) getButton.click();
                getButton.click()
            }, 100);
            time = setInterval(async () => {
                const queryButton = iframeDocument.getElementsByClassName("el-button query-btn el-button--primary")[0];
                let getButton = iframeDocument.getElementsByClassName("el-button el-button--default el-button--small")[0];
                let num = iframeDocument.getElementsByClassName("num")[0];
                if (getButton.disabled) {
                    return;
                }
                if (num.innerText.includes("请稍等")) {
                    return;
                }
                lis[i].click();
                queryButton.click();
                const n = parseInt(num.innerText.match(regex)[0]);
                if (n == 0) {
                    i++;
                    if (i === checks.length) i = 0;
                }
            }, ms + 200);
            cardBtn.innerText = "(运行中)点击打开面板"
            dialog.close()
        }

        cardBtn.oncontextmenu = begin
        document.getElementById("locnelorAdd").onclick = () => {
            textarea.value = `${textarea.value}\n${select.value}`
        }
        document.getElementById("locnelorBeginBtn").onclick = begin
        document.getElementById("locnelorCloseBtn").onclick = () => {
            clearInterval(time);
            clearInterval(time2)
            running = false;
            cardBtn.innerText = "点击打开面板 或右键启动"
            dialog.close()
        }
    }
    main()
})()